package com.example.feature;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//thenapply 只会有无异常情况，需要yoghurt  exceplate 单独去获取异常

//后续任务如果不需要返回信息则可以直接使用 thenAcceptAsync
//thenRunAsync 不需要观心前置任务的返回会直接执行
public class HandleDemo {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);

        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            try {
//                int i = 1/0; //异常抛出
                Thread.sleep(300L);
                System.out.println(Thread.currentThread().getName());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 123;
            //thenApply依赖任务执行是同一个线程
            //thenApplyAsync 会使用一个新的线程
        }, executorService).handleAsync((value, throwable)->{
            int res = 1;
            if(throwable == null){
                //前置任务没有异常
                res = value * 10;
                System.out.println(res);
            }else {
                //前置任务异常
                System.out.println(throwable.getMessage());
            }
            return res;

        }, executorService);

        executorService.shutdown();
    }
}
